import Vue from 'vue'
import Router from 'vue-router'
// 导入登录组件
import Login from './components/login.vue'
//导入home组件
import Home from './components/home.vue'
// 导入欢迎组件
import Welcome from './components/welcome.vue'
// 导入用户列表组件
import Users from './components/users/users.vue'
// 导入Rights权限组件
import Rights from './components/power/rights.vue'
// 导入Roles角色组件
import Roles from './components/power/roles.vue'
// 导入商品分类组件Cate
import Cate from './components/goods/cate.vue'
// 导入 商品参数 组价
import Params from './components/goods/params.vue'
// 导入商品列表组件
import GoodsList from './components/goods/list.vue'
// 添加商品 的页面组件
import AddGoods from './components/goods/add.vue'
// 导入订单order组件
import Order from './components/order/order.vue'
// 导入报表组件
import Report from './components/report/report.vue'


// 挂载router路由
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path:'/users',component: Users},
        { path:'/rights',component: Rights },
        { path:'/roles',component: Roles },
        { path:'/categories',component: Cate },
        { path:'/params',component: Params },
        { path:'/goods',component: GoodsList },
        { path:'/goods/add',component: AddGoods },
        { path:'/orders',component: Order },
        { path:'/reports',component: Report },
      ]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from代表从哪个路径来
  // next是一个函数，表示放行
  // next()放行  next('/路径')强制跳转的路径
  if (to.path === '/login') {
    return next();//直接放行
  } else {
    // 获取token
    const token = window.sessionStorage.getItem('token');
    if (token) {
      return next();
    } else {
      // 没有token强制跳转到登录页
      return next('/login');
    }
  }

})

export default router